﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;

public partial class change_reservation : System.Web.UI.Page
{
    OleDbConnection conx;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar2.SelectedDate = Calendar1.SelectedDate = DateTime.Now;
        }

        //Label1.Text = Calendar1.SelectedDate.ToString("dd-MMM-yyyy");
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            label_new_in_date.Visible = true;
            label_new_out_date.Visible = true;
        }
        else
        {
            label_new_in_date.Visible = false;
            label_new_out_date.Visible = false;
        }
    }
    
    protected void Button_Reservation_Click(object sender, EventArgs e)
    {
        Response.Redirect("home.aspx");
    }
    protected void Button_Edit_Click(object sender, EventArgs e)
    {
        if(GridView1.SelectedIndex == -1){
            Label1.Text = "Please Select Reservation before edit.";
            return;
        }

        MultiView1.ActiveViewIndex = 1;

        Label2.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
        Label3.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
        Label4.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text;
        Label5.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
        Label6.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text;

        Calendar1.SelectedDate = Convert.ToDateTime(GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text);
        Calendar2.SelectedDate = Convert.ToDateTime(GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text);
    }
    protected void Change_Status_Click(object sender, EventArgs e)
    {
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            Label7.Visible = true;
            Label7.Text = "Please Select Check in and Check out Date<br />(Check in before Check out) <br/>";
            label_new_in_date.Visible = true;
            label_new_out_date.Visible = true;
            return;
        }
        else
        {
            label_new_in_date.Visible = false;
            label_new_out_date.Visible = false;
        }


        try
        {
            String con = "Provider=MSDAORA;Data Source=orcl;Password=oracle;User ID=pearl";//"DSN=orcl;Driver={Microsoft ODBC for Oracle};UID=system;PWD=password;SERVER=localhost;";
            conx = new OleDbConnection(con);
            conx.Open();

            if (conx.State == ConnectionState.Open)
            {
                string ddl1 = DropDownList1.SelectedValue;

                OleDbCommand ODC = new OleDbCommand("UPDATE RESERVATIONS SET STATUS = '" + ddl1 + "' , CHECKIN_DATE = '" + Calendar1.SelectedDate.ToString("dd-MMM-yyyy") + "' , CHECKOUT_DATE = '"+Calendar2.SelectedDate.ToString("dd-MMM-yyyy")+"'   WHERE ID = '" + Convert.ToInt32(GridView1.SelectedIndex + 1) + "'", conx);
                ODC.ExecuteNonQuery();
                OleDbCommand ODC2 = new OleDbCommand("COMMIT", conx);
                ODC2.ExecuteNonQuery();
                Label7.Text = "\" Status Data No." + (GridView1.SelectedIndex + 1) + "\" " + ddl1 + "\"" + "UPDATED\"";
            }
            else
                Label1.Text = Label7.Text = "Oracle Connect Failed";
        }

        catch (Exception ex)
        {
            Label7.Text = ex.Message + "<br />" + ex.StackTrace;
        }

        finally
        {
            conx.Close();
            conx = null;
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            label_new_in_date.Visible = true;
            label_new_out_date.Visible = true;
        }
        else
        {
            label_new_in_date.Visible = false;
            label_new_out_date.Visible = false;
        }
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            label_new_in_date.Visible = true;
            label_new_out_date.Visible = true;
        }
        else
        {
            label_new_in_date.Visible = false;
            label_new_out_date.Visible = false;
        }
    }
    protected void Back_Click(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 1)
        {
            MultiView1.ActiveViewIndex = 0;
        }
        else
        Response.Redirect("reservation.aspx");
    }
}